<?php
require_once('../models/Model.php');

class User extends Model{

    public function find($username)
    {
        $table = 'blog_user';

        //找出user的数据,有就跳转,没有就创建并跳转
        $mysql = $this->mysql;

        $sql = "select * from blog_user where username like '".$username."'";

        //查询
        $res = $mysql->GetOne($sql);

        if($res == false){
            //没有,insert
            $data = [
                'username' => $username,
            ];
            $id = $mysql->Insert($table,$data);
            if($id == -1){
                //插入失败
                return false;
                exit();
            }
        }else{
            //有,查出用户信息
            $id = $res[0];
            $username = $res[1];
        }

        $mysql->close_connect();

        $_SESSION['username'] = $username;
        $_SESSION['userid'] = $id;

        return 1;
    }
}
